<?php

/**
* 股票板块
*
* @package		Hooloo framework
* @author 		Bill
* @copyright 	Hooloo Co.,Ltd
* @version		1.0
* @release		2017.05.25
*/
defined('BASEPATH') OR exit('No direct script access allowed');

class Config  extends Controller{
	public function __construct(){
		parent::__construct();
	}
	
	/**
	* 股票板块列表
	*/
	public function index($class = 0, $bk_name = 0 , $p = 1 ){
		//通用搜索
		$where = " where 1=1";
		
		//板块分类
		$class = (int)$class < 0 ? 0 : $class;
		if($class){
			$where .= " AND class = '$class'";
		} else {
			$class = 0;
		}
		
		//板块名称
		$bk_name = sql_format($bk_name);
		if($bk_name){
			$where .= " AND bk_name like  '%$bk_name%'";
		} else {
			$bk_name = 0;
		}
		
		//数据分页
		$p = (int)$p <= 0 ? 1 : (int)$p;
		$pize = 15;
		$start = ($p - 1) * $pize;
		
		//统计总数
		$sql = "select count(*) as c from config_stock" . $where;
		$total = $this->db->query($sql)->row_array();
		$total = $total["c"];
		
		//分页查询
		$sql = "select *  from config_stock" . $where . " order by id DESC limit $start, $pize";
		$list = $this->db->query($sql)->result_array();
		
		//分页展示
		$url = "/" . $this->_controller . "/" . $this->_action  . "/" . $class. "/" . $bk_name . "/";
		$page = page_format($total, $pize, $p, $url);
		
		$class_list = array(1=>'地域',2=>'行业',3=>'概念',4=>'指数板块',5=>'证监会行业',6=>'其他板块');
		
		//分配展示
		$this->assign("list", $list);
		$this->assign("page", $page);
		$this->assign("total", $total);
		$this->assign("class", $class);
		$this->assign("bk_name", $bk_name);
		$this->assign("class_list", $class_list);
		
		$this->display();
	}
	
	/**
	* 新增股票板块
	* @param    int  	$id   		id
	* @param    char  	$class   	分类
	* @param    char  	$bk_name   	版块名称
	* @param    int  	$add_time   编辑时间
	*/
	public function add(){
		if(IS_POST){
			$class =  isset($_POST["class"]) ? (int)$_POST["class"] : 0;
			$bk_name = isset($_POST["bk_name"]) ? substr(sql_format($_POST["bk_name"]), 0, 100) : "";
			
			//判断分类
			if(! $class){
				ajax_return(0, "请选择分类");
			}
			//判断版块名称
			if(! $bk_name){
				ajax_return(0, "请输入版块名称");
			}

			//查询记录是否存在
			$sql = "select id from config_stock where bk_name = '$bk_name' and class = '$class'";
			$result = $this->db->query($sql)->row_array();
			$result && ajax_return(0, "版块名称已经存在");
			
			//插入新的记录
			$sql = "insert into config_stock (class,bk_name,add_time) values ('$class','$bk_name','" . time() ."')";
			$this->db->query($sql);
			$res =$this->db->insert_id();
			if(! $res){
				ajax_return( 0, "添加失败" );
			} else{
				ajax_return( 1, "添加成功" , "/config" );
			}
		}
		$class_list = array(1=>'地域',2=>'行业',3=>'概念',4=>'指数板块',5=>'证监会行业',6=>'其他板块');
		$this->assign("class_list",$class_list);
		$this->display();
	}
	
	/**
	* 编辑股票板块
	* @param    int  	$id   		id
	* @param    char  	$class   	分类
	* @param    char  	$bk_name   	版块名称
	* @param    int  	$add_time   编辑时间
	*/
	public function edit($id = 0){
		if(IS_POST){
			
			if(0 >= $id){
				ajax_return( 0 , "参数有误" );
			}
			$class =  isset($_POST["class"]) ? $_POST["class"] : "0";
			$bk_name = isset($_POST["bk_name"]) ? substr(sql_format($_POST["bk_name"]), 0, 100) : "";
			 
			
			//判断分类
			if(! $class){
				ajax_return(0, "请选择分类");
			}
			//判断版块名称
			if(! $bk_name){
				ajax_return(0, "请输入版块名称");
			}
			
			//查询记录是否存在
			$sql = "select id from config_stock where id <> $id AND bk_name = '$bk_name' AND class = '$class'";
			$result = $this->db->query($sql)->row_array();
			$result && ajax_return(0, "版块名称已经存在");
			
			//更新记录
			$sql = "update config_stock set class = '$class',bk_name = '$bk_name',add_time = '" . time() . "' where id = $id";
			$this->db->query($sql);
			
			//返回结果集
			ajax_return( 1, "更新成功" , "/config");
		} else{
			if(0 >= $id){
				$this->error("参数有误");
			}
			$sql = "select * from config_stock where id = $id";
			$result = $this->db->query($sql)->row_array();
			if(! $result){
				$this->error("记录不存在");
			}
			$class_list = array(1=>'地域',2=>'行业',3=>'概念',4=>'指数板块',5=>'证监会行业',6=>'其他板块');
			$this->assign("class_list",$class_list);
			$this->assign("result", $result);
			$this->display();
		}
	}
	
	/**
	* 删除股票板块
	* @param    int  	$id   		id
	*/
	public function del($id = 0){
		if(IS_POST){
			!$id &&	ajax_return(0, "参数有误");
			$sql = "delete from  config_stock where id in ($id)";
			$this->db->query($sql);
			ajax_return(1, "删除成功");
		}
	}
}
